package org.bizming.web.action;

import java.io.IOException;
import java.util.List;

import org.bizming.base.BaseErrCode;
import org.bizming.base.BaseException;
import org.bizming.medicine.model.help.MedicineUnit;
import org.bizming.medicine.service.MedicineUnitService;
import org.bizming.web.util.WebUtil;

public class MedicineUnitAction {

	private MedicineUnitService unitService;

	private MedicineUnit mu;

	/** 修改数据 */
	public String addOrUpdate() {
		int errCode = BaseErrCode.SUCCESS;

		try {
			if (null != mu && null == mu.getId()) {
				unitService.add(mu);
			} else {
				unitService.update(mu);
			}

			WebUtil.getResponse().sendRedirect(
					WebUtil.getRequest().getContextPath() + "/medicineUnit!selectList.action");
			return null;
		} catch (BaseException e) {
			errCode = e.getErrCode();
		} catch (Exception e) {
			e.printStackTrace();
			errCode = BaseErrCode.SERVER_ERROR;
		}
		WebUtil.getRequest().setAttribute("code", errCode);
		WebUtil.getRequest().setAttribute("o", mu);
		return "addOrUpdate_exception";
	}

	/** 查看数据 */
	public String view() {
		MedicineUnit t = null;
		if (null != mu && null != mu.getId()) {
			t = unitService.get(mu.getId());
		}
		WebUtil.getRequest().setAttribute("o", t);
		return "view";
	}

	/**
	 * 删除数据
	 * 
	 * @throws IOException
	 */
	public String remove() throws IOException {

		if (null != mu && null != mu.getId()) {
			unitService.remove(mu.getId());
		}

		WebUtil.getResponse().sendRedirect(
				WebUtil.getRequest().getContextPath() + "/medicineUnit!selectList.action");
		return null;
	}

	/** 获取数据 */
	public String selectList() {

		List<MedicineUnit> list = unitService.selectList();
		WebUtil.getRequest().setAttribute("list", list);

		return "selectList";
	}

	// ===============================================

	public MedicineUnitService getUnitService() {
		return unitService;
	}

	public void setUnitService(MedicineUnitService unitService) {
		this.unitService = unitService;
	}

	public MedicineUnit getMu() {
		return mu;
	}

	public void setMu(MedicineUnit mu) {
		this.mu = mu;
	}

}
